package com.prism.e4.rcp.ui.parts;

import java.io.File;

import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.ViewerCell;

class ViewLabelProvider extends StyledCellLabelProvider {
    @Override
    public void update(ViewerCell cell) {
      Object element = cell.getElement();
      StyledString text = new StyledString();
      File file = (File) element;
      if (file.isDirectory()) {
        text.append(getFileName(file));
        String[] files = file.list();
        if (files != null) {
          text.append(" (" + files.length + ") ",
              StyledString.COUNTER_STYLER);
        }
      } else {
        text.append(getFileName(file));
      }
      cell.setText(text.toString());
      cell.setStyleRanges(text.getStyleRanges());
      super.update(cell);

    }

    private String getFileName(File file) {
      String name = file.getName();
      return name.isEmpty() ? file.getPath() : name;
    }
  }
